package systeminfo

import (
	"fmt"
	"github.com/shirou/gopsutil/cpu"
	"time"
)

func GetCpuStatus() Cpu {
	var cpuInfo Cpu
	idle0, total0 := getCPUSample()
	time.Sleep(3 * time.Second)
	idle1, total1 := getCPUSample()

	idleTicks := float64(idle1 - idle0)
	totalTicks := float64(total1 - total0)
	cpuUsage := 100 * (totalTicks - idleTicks) / totalTicks

	cpuInfo.CpuUsage = fmt.Sprintf("%.2f", cpuUsage)
	cpuInfo.Busy = fmt.Sprintf("%.2f", totalTicks-idleTicks)
	cpuInfo.Total = fmt.Sprintf("%.2f", totalTicks)
	return cpuInfo

}

type Win32_PerfFormattedData_PerfOS_Processor struct {
	PercentProcessorTime uint64
}

func getCPUSample() (uint64, uint64) {
	percentages, err := cpu.Percent(time.Second, false)
	if err != nil {
		return 0, 0
	}

	idle := uint64(percentages[0])
	total := uint64(100)

	for _, p := range percentages[1:] {
		total -= uint64(p)
	}

	return idle, total
}
